package com.sikaryofficial.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.ReloadActionDataCache;
import com.sikaryofficial.backend.annotation.Repeat;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.constant.ProductStyleEnum;
import com.sikaryofficial.backend.domain.dto.req.BrandTasteEvaluateListReq;
import com.sikaryofficial.backend.domain.dto.req.BrandTasteEvaluateOwnerReq;
import com.sikaryofficial.backend.domain.dto.req.BrandTasteEvaluateReq;
import com.sikaryofficial.backend.domain.dto.resp.BrandTasteEvaluateDTO;
import com.sikaryofficial.backend.domain.dto.resp.EvaluateTabTopData;
import com.sikaryofficial.backend.domain.dto.resp.IndexStatisticsOutputDTO;
import com.sikaryofficial.backend.manager.BrandTasteEvaluateManager;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.core.web.domain.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 品牌口味评价Controller
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/brandTasteEvaluate")
@Api(tags = "品牌口味评价")
public class BrandTasteEvaluateController extends BaseController {

    @Autowired
    private BrandTasteEvaluateManager brandTasteEvaluateManager;

    /**
     * 查询品牌口味评价列表
     * 2、支持按产品评价分页列表
     *
     * @return
     */
    @ApiOperation("个人中心-我的评价列表)")
    @PostMapping("/ownerList")
    public PageResult<BrandTasteEvaluateDTO> ownerList(@Validated @RequestBody BrandTasteEvaluateOwnerReq req) {
        IPage<BrandTasteEvaluateDTO> page = brandTasteEvaluateManager.ownerList(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 查询品牌口味评价列表
     * 2、支持按产品评价分页列表
     *
     * @return
     */
    @ApiOperation("评论分页列表(评价详情)")
    @PostMapping("/listPage")
    @Repeat
    public PageResult<BrandTasteEvaluateDTO> listPage(@Validated @RequestBody BrandTasteEvaluateListReq req) {
        IPage<BrandTasteEvaluateDTO> page = brandTasteEvaluateManager.listPage(req);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 查询品牌口味评价列表
     * 最新评论
     *
     * @return
     */
    @ApiOperation("首页-最新评论")
    @GetMapping("/latest")
    public HiResult<List<BrandTasteEvaluateDTO>> latestList() {
        return HiResultUtil.success(brandTasteEvaluateManager.latestList());
    }

    /**
     * 产品评价指标统计
     *
     * @param brandTasteId 品牌口味ID
     * @return
     */
    @ApiOperation("产品评价指标统计")
    @GetMapping("/indexStatistics/{brandTasteId}")
    @Repeat
    public HiResult<IndexStatisticsOutputDTO> evaluateIndexStatistics(@PathVariable(name = "brandTasteId", required = false) Object brandTasteId) {
        if (Objects.isNull(brandTasteId)) {
           throw new ServiceException("product is not found",404);
        }
        return HiResultUtil.success(brandTasteEvaluateManager.evaluateIndexStatistics(Long.parseLong(brandTasteId.toString())));
    }

    /**
     * 评价页签： 根据款式进行数据过滤
     * 款式: style:disposable 一次性,cartridge 弹药筒,opentype 开放式
     *
     * @param style
     * @return
     */
    @ApiOperation("评价页签查询")
    @GetMapping("/top/{style}")
    @ReloadActionDataCache
    @Repeat
    public HiResult<EvaluateTabTopData> topByStyle(@PathVariable(name = "style") String style) {
        ProductStyleEnum styleEnum = ProductStyleEnum.getByCode(style);
        if (Objects.isNull(styleEnum)) {
            throw new ServiceException("param style is not exist.");
        }
        return HiResultUtil.success(brandTasteEvaluateManager.topByStyle(style));
    }

    /**
     * 查询评价详情
     *
     * @param evaluateId
     * @return
     */
    @ApiOperation("评论详情")
    @GetMapping("/detail/{evaluateId}")
    @TenantFilter
    @Repeat
    public HiResult<BrandTasteEvaluateDTO> detail(@PathVariable(name = "evaluateId") Object evaluateId) {
        if (Objects.isNull(evaluateId)){
            return HiResultUtil.error("evaluateId is null");
        }
        if (Long.parseLong(evaluateId.toString()) <= 0 ){
            return HiResultUtil.error("evaluateId is invalid");
        }
        return HiResultUtil.success(brandTasteEvaluateManager.detail(Long.parseLong(evaluateId.toString())));
    }

    /**
     * 新增品牌口味评价
     */
    @ApiOperation("添加评论")
    @PostMapping("/add")
    @Repeat
    public HiResult<Boolean> add(@RequestBody BrandTasteEvaluateReq brandTasteEvaluate) {
        return HiResultUtil.success(brandTasteEvaluateManager.saveEvaluate(brandTasteEvaluate));
    }
}
